#include "stdafx.h"
#include "CacheSettingsDlg.h"
#include <xtd/string>
#include "mswin/file_dir.h"
#include ".\cachesettingsdlg.h"

IMPLEMENT_DYNAMIC(CacheSettingsDlg, CDialog)

CacheSettingsDlg::CacheSettingsDlg(modal, icache::panel & p, CWnd * pParent) :
	_panel(p), CDialog(CacheSettingsDlg::IDD, pParent)
{
	_changed = false;
}

CacheSettingsDlg::CacheSettingsDlg(modaless, icache::panel & p, CWnd * pParent) :
	_panel(p)
{
	_changed = false;
	VERIFY(Create(CacheSettingsDlg::IDD, pParent));
}

CacheSettingsDlg::~CacheSettingsDlg()
{
}

BEGIN_MESSAGE_MAP(CacheSettingsDlg, CDialog)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_PURGE_MEM_CACHE, OnBnClickedPurgeMemCache)
	ON_BN_CLICKED(IDC_PURGE_DISC_CACHE, OnBnClickedPurgeDiscCache)
	ON_BN_CLICKED(IDC_DISC_CACHE_ERASE_ON_EXIT, OnBnClickedDiscCacheEraseOnExit)
	ON_EN_SETFOCUS(IDC_MEM_CACHE_QUOTA, OnEditFocused)
	ON_EN_SETFOCUS(IDC_DISC_CACHE_QUOTA, OnEditFocused)
	ON_EN_SETFOCUS(IDC_AVI_PREFETCH_FRAMES, OnEditFocused)
	ON_BN_CLICKED(IDC_APPLY, OnBnClickedApply)
	ON_BN_CLICKED(IDOK, OnBnClickedOk)
	ON_BN_CLICKED(IDC_CHANGE_ROOT_PATH, OnBnClickedChangeRootPath)
END_MESSAGE_MAP()

void CacheSettingsDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BOOL CacheSettingsDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	RetrieveCacheSettings();
	SetTimer(1, 1000, 0);

	return TRUE;  // return TRUE unless you set the focus to a control
	// EXCEPTION: OCX PropertyBase Pages should return FALSE
}

template <class T>
inline xtd::string megabytes(T i)
	{ return xtd::string(ceil(double(i)*10 / (1024*1024)) / 10); }

void CacheSettingsDlg::UpdateControls(icache::panel::data const & d)
{
	SetDlgItemText(IDC_SYSTEM_RAM, megabytes(_panel.physical_memory()).c_str());
	SetDlgItemText(IDC_MEM_CACHE_QUOTA, megabytes(d.mem_cache_quota).c_str());
	SetDlgItemText(IDC_MEM_CACHE_SIZE, megabytes(d.mem_cache_size).c_str());
	SetDlgItemInt(IDC_MEM_CACHE_ITEMS, d.mem_cache_items);

	SetDlgItemText(IDC_DISC_CACHE_ROOT_PATH, d.disc_cache_root_path.c_str());
	CheckDlgButton(IDC_DISC_CACHE_ERASE_ON_EXIT, d.disc_cache_erase_on_exit ? 1 : 0);
	SetDlgItemText(IDC_DISC_CACHE_QUOTA, megabytes(d.disc_cache_quota).c_str());
	SetDlgItemText(IDC_DISC_CACHE_SIZE, megabytes(d.disc_cache_size).c_str());
	SetDlgItemInt(IDC_DISC_CACHE_ITEMS, d.disc_cache_items);

	SetDlgItemInt(IDC_AVI_PREFETCH_FRAMES, d.avi_prefetch_frames);
	Changed(false);
}

void CacheSettingsDlg::RetrieveCacheSettings()
{
	icache::panel::data d;
	_panel.get_data(d);
	UpdateControls(d);
}

template <class T>
void CacheSettingsDlg::GetMegabytes(int nID, T & i) 
{
	char buf[32];
	GetDlgItemText(nID, buf, 32);
	i = xtd::string(buf).to<double>() * (1024*1024);
}

void CacheSettingsDlg::SetCacheSettings()
{
	icache::panel::data d;
	GetMegabytes(IDC_MEM_CACHE_QUOTA, d.mem_cache_quota);

	d.disc_cache_root_path.resize(MAX_PATH);
	d.disc_cache_root_path.resize(
		GetDlgItemText(IDC_DISC_CACHE_ROOT_PATH, &d.disc_cache_root_path[0], MAX_PATH));
	d.disc_cache_erase_on_exit = IsDlgButtonChecked(IDC_DISC_CACHE_ERASE_ON_EXIT) != 0;
	GetMegabytes(IDC_DISC_CACHE_QUOTA, d.disc_cache_quota);

	d.avi_prefetch_frames = GetDlgItemInt(IDC_AVI_PREFETCH_FRAMES);
	_panel.set_data(d);
	_panel.remember(d);
	UpdateControls(d);
}

void CacheSettingsDlg::OnTimer(UINT nIDEvent)
{
	if (!_changed)
		RetrieveCacheSettings();

	CDialog::OnTimer(nIDEvent);
}

void CacheSettingsDlg::OnBnClickedPurgeMemCache()
{
	_panel.purge_mem_cache();
	RetrieveCacheSettings();
}

void CacheSettingsDlg::OnBnClickedPurgeDiscCache()
{
	_panel.purge_disc_cache();
	RetrieveCacheSettings();
}

void CacheSettingsDlg::OnBnClickedDiscCacheEraseOnExit()
{
	Changed(true);
}

void CacheSettingsDlg::OnEditFocused()
{
	Changed(true);
}

void CacheSettingsDlg::OnBnClickedApply()
{
	SetCacheSettings();
}

void CacheSettingsDlg::OnBnClickedOk()
{
	SetCacheSettings();
	OnOK();
}

void CacheSettingsDlg::OnBnClickedChangeRootPath()
{
	CString dir;
	GetDlgItemText(IDC_DISC_CACHE_ROOT_PATH, dir);
	dir = mswin::parent((const char *)dir).c_str(); //because dir is locked by disc_cache
	CFileDialog dlg(FALSE, "png", "image.png",
		OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT | OFN_NOCHANGEDIR, 
		"Cached Images (*.png)|*.png||", this);

	dlg.m_ofn.lpstrTitle = "Choose Directory for Disc Cache Deployment"; 
	dlg.m_ofn.lpstrInitialDir = dir;
	if (dlg.DoModal() != IDOK) 
		return;

	SetDlgItemText(IDC_DISC_CACHE_ROOT_PATH, 
		mswin::parent(std::string(dlg.GetPathName())).c_str());
	Changed(true);
}

void CacheSettingsDlg::Changed(bool f)
{
	_changed = f;
	GetDlgItem(IDC_APPLY)->EnableWindow(_changed);
}
